package com.lcx.db137.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lcx.db137.service.UserroleService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcx.db137.entity.Userrole;
import com.lcx.db137.mapper.UserroleMapper;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;
import java.util.Map;

/**
 * @Description  
 * @version 1.0 
 * @author lcx
 * @date 2024/12/24 11:48
 */
@Service
public class UserroleServiceImpl extends ServiceImpl<UserroleMapper, Userrole> implements UserroleService {

    @Transactional
    @Override
    public boolean updateRoleIdByUserId(Integer userId, List<Integer> roleId) {
        // 1.根据用户id删除这个用户的所有角色
        UpdateWrapper<Userrole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userid", userId);
        baseMapper.delete(updateWrapper);
        // 2.根据用户id添加新的角色
        for (Integer roleid : roleId){
            Userrole userrole = new Userrole();
            userrole.setUserid(userId);
            userrole.setRoleid(roleid);
            baseMapper.insert(userrole);
        }
        return true;
    }
}
